<template>
    <div class="login-container">
        <el-card class="login-card">
            <template #header>
                <div class="card-header">
                    <span>S3 Browser 登录</span>
                    <small>联通云对象存储</small>
                </div>
            </template>

            <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-position="top"
                @submit.prevent="handleLogin">
                <el-form-item label="Access Key ID" prop="access_key_id">
                    <el-input v-model="loginForm.access_key_id" placeholder="请输入您的 Access Key ID" size="large"
                        clearable />
                </el-form-item>
                <el-form-item label="Secret Access Key" prop="secret_access_key">
                    <el-input v-model="loginForm.secret_access_key" type="password" show-password
                        placeholder="请输入您的 Secret Access Key" size="large" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleLogin" :loading="isLoading" style="width: 100%;"
                        size="large">
                        登 录
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useAuthStore } from '@/stores';

const router = useRouter();
const authStore = useAuthStore();

const loginFormRef = ref(null);
const isLoading = ref(false);

const loginForm = reactive({
    access_key_id: '',
    secret_access_key: '',
});

const rules = reactive({
    access_key_id: [{ required: true, message: 'Access Key ID 不能为空', trigger: 'blur' }],
    secret_access_key: [{ required: true, message: 'Secret Access Key 不能为空', trigger: 'blur' }],
});

const handleLogin = async () => {
    if (!loginFormRef.value) return;
    await loginFormRef.value.validate(async (valid) => {
        if (valid) {
            isLoading.value = true;
            try {
                await authStore.login({
                    access_key_id: loginForm.access_key_id,
                    secret_access_key: loginForm.secret_access_key,
                });
                ElMessage.success('登录成功！');

                // 登录成功后，跳转到之前想去的页面，如果没指定就去首页
                const redirectPath = router.currentRoute.value.query.redirect || '/';
                router.push(redirectPath);

            } catch (error) {
                ElMessage.error(error.message);
            } finally {
                isLoading.value = false;
            }
        }
    });
};
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f0f2f5;
}

.login-card {
    width: 100%;
    max-width: 400px;
    margin: 0 20px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.card-header span {
    font-size: 20px;
    font-weight: bold;
}

.card-header small {
    color: #909399;
}
</style>